package cn.kgc.springboot.controller;

import cn.kgc.springboot.entity.Dept;
import cn.kgc.springboot.service.DeptService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.lang.model.element.VariableElement;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: mosin
 * @version: v1.0  2022/7/31
 */
@RestController
@RequestMapping("dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping
    public Map getAll(){
        HashMap<String, Object> map = new HashMap<>();
        // 判定缓存数据
        Boolean aBoolean = redisTemplate.hasKey("deptList");
        if(aBoolean){
            Object deptList = redisTemplate.opsForValue().get("deptList");
            map.put("code", 200);
            map.put("data", deptList);
            map.put("msg", "数据来自redis");

        }else{
            //查询mysql数据库
            List<Dept> list = deptService.list();
            if(!ObjectUtils.isEmpty(list)){
                redisTemplate.opsForValue().set("deptList", list);
            }
            map.put("code", 200);
            map.put("msg", "数据来自mysql");
            map.put("data", list);
        }
        return  map;
    }

    @DeleteMapping("{id}")
    public  Map  deleteById(@PathVariable Integer id){
        HashMap<String, Object> map = new HashMap<>();
        boolean b = deptService.removeById(id);
        // 判定删除是否成功
        if(b){
            //判定缓存数据  删除缓存
            if(redisTemplate.hasKey("deptList")){  //  清空缓存
                redisTemplate.delete("deptList");
            }
        }
        map.put("code", 200);
        map.put("msg", "数据已删除");
        return  map;
    }

    @PostMapping
    public  Map  addDept(@RequestBody Dept dept){

        HashMap<String, Object> map = new HashMap<>();
        boolean save = deptService.save(dept);
        // 判定删除是否成功
        if(save){
            //判定缓存数据  删除缓存
            if(redisTemplate.hasKey("deptList")){  //  清空缓存
                redisTemplate.delete("deptList");
            }
        }

        map.put("code", 200);
        map.put("msg", "数据保存成功");
        return  map;
    }

    @PutMapping
    public  Map  updateDept(@RequestBody Dept dept){
        HashMap<String, Object> map = new HashMap<>();
        UpdateWrapper<Dept> deptUpdateWrapper = new UpdateWrapper<>();
        deptUpdateWrapper.eq("deptno", dept.getDeptno());
        boolean update = deptService.update(dept,  deptUpdateWrapper);
        // 判定删除是否成功
        if(update){
            //判定缓存数据  删除缓存
            if(redisTemplate.hasKey("deptList")){  //  清空缓存
                redisTemplate.delete("deptList");
            }
        }

        map.put("code", 200);
        map.put("msg", "数据更新成功");
        return  map;
    }


}
